#region File Description
//-----------------------------------------------------------------------------
// ChaseCamera.cs
//
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.
//-----------------------------------------------------------------------------
#endregion

#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using MCGameEngine.Physic_Layer;

#endregion

namespace MCGameEngine
{
    public class Camera
    {
        public float nearClip;
        public float farClip;
        public float aspectRatio;
        PhysicObject protagonist;

        public PhysicObject Protagonist
        {
            get
            {
                return protagonist;
            }
            set
            {
                if (protagonist == null)
                {
                    protagonist = value;
                }
            }
        }

        float viewangle;
        public float viewAngle
        {
            get
            {
                return MathHelper.ToDegrees(viewangle);
            }
            set
            {
                viewangle = MathHelper.ToRadians(value);
            }
        }

        Matrix view;

        public Matrix View
        {
            get
            {
                return view;
            }
        }

        Matrix projection;

        public Matrix Projection
        {
            get
            {
                return projection;
            }
        }

        Vector3 thirdPersonReference;

        public float Distance
        { 
            get 
            {
                return thirdPersonReference.Y;
            }

            set
            {
                thirdPersonReference = new Vector3(0,value,value);
            }
        }

        public Camera()
        {
            viewAngle = MathHelper.ToRadians(180);
            nearClip = 20;
            farClip = 100000.0f;
            thirdPersonReference = new Vector3(0, -1000, -1000);
        }

        public void Update()
        {
            if (Protagonist != null)
            {
                Matrix rotationMatrix = Matrix.CreateRotationY(Protagonist.Rotation.Y);
                Vector3 transformedReference = Vector3.Transform(thirdPersonReference, rotationMatrix);
                Vector3 cameraPosition = Protagonist.Position - transformedReference;

                view = Matrix.CreateLookAt(cameraPosition, Protagonist.Position, new Vector3(0.0f, 1.0f, 0.0f));
                projection = Matrix.CreatePerspectiveFieldOfView(viewangle, aspectRatio, nearClip, farClip);
            }
        }
    }
}
